package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnLikesBehaviorServiceImpl implements ApUnLikesBehaviorService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存不喜欢的信息
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {
        //1.检验参数
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.获取登录人的信息
        ApUser user = AppThreadUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //3判断用户是否不喜欢,查询redis
        //3.1获取关注缓存hash类型的key
        String unLike_hash_key = "USER_UNLIKE_" + dto.getArticleId();
        //3.2判断是否已经不喜欢,查询缓存中的小key是否包含点赞人的id
        Boolean hasKey = redisTemplate.opsForHash().hasKey(unLike_hash_key, user.getId() + "");

        //用户未标记不喜欢
        if(!hasKey && dto.getType()==0){//要执行的是不喜欢操作
        //    保存数据到redis中
            redisTemplate.opsForHash().put(unLike_hash_key,user.getId()+"","1");
        //    保存到mongodb中
            ApUnlikesBehavior apUnlikesBehavior=new ApUnlikesBehavior();
            apUnlikesBehavior.setUserId(user.getId().longValue());
            apUnlikesBehavior.setType(dto.getType());
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            mongoTemplate.insert(apUnlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        //用户已标记不喜欢
        if(hasKey && dto.getType()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.UNLIKES_USER_SUCCESS);
        }
        if(hasKey && dto.getType()==1){
        //    删除缓存数据
            redisTemplate.opsForHash().delete(unLike_hash_key,user.getId()+"");
        //    删除mongodb的数据
            Query query=Query.query(Criteria.where("userId").is(user.getId()).and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query,ApUnlikesBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.UNLIKES_CANCEL_SUCCESS);
        }
        return null;
    }
}
